//test browser type
var user = navigator.userAgent;
var browser = {};
browser.opera = user.indexOf("Opera") > -1 && typeof window.opera == "object";
browser.ie = user.indexOf("MSIE") > -1 && !browser.opera;
browser.ff = user.indexOf("Firefox") > -1;
browser.chrome = user.indexOf("Chrome") > -1;
browser.safari = user.indexOf("Safari") > -1;
function loginover(o)
{
	if (browser.ie)
    {
		o.style.backgroundImage = "url('ico/reg_button_mouseon.png')";
    }
	else
		o.setStyle('background-image', 'url(ico/reg_button_mouseon.png)');	
}

function loginup(o)
{
	if (browser.ie)
		o.style.backgroundImage = "url('ico/reg_button_mouseon.png')";
	else
		o.setStyle('background-image', 'url(ico/reg_button_mouseon.png)');	
}

function logindown(o)
{
	if (browser.ie)
		o.style.backgroundImage = "url('ico/back_front_page.png')";
	else
		o.setStyle('background-image', 'url(ico/back_front_page.png)');	
}

function loginout(o)
{
	if (browser.ie)
		o.style.backgroundImage = "";
	else
		o.setStyle('background-image', '');	
}

function logoutover(o)
{
	if (browser.ie)
		o.style.backgroundImage = "url('ico/reg_button_mouseon.png')";
	else
		o.setStyle('background-image', 'url(ico/reg_button_mouseon.png)');	
    //$j('#logoutword').css('color', 'rgb(242, 242, 242)');
}

function logoutup(o)
{
	if (browser.ie)
		o.style.backgroundImage = "url('ico/reg_button_mouseon.png')";
	else
		o.setStyle('background-image', 'url(ico/reg_button_mouseon.png)');	
}

function logoutdown(o)
{
	if (browser.ie)
		o.style.backgroundImage = "url('ico/reg_button_click.png')";
	else
		o.setStyle('background-image', 'url(ico/reg_button_click.png)');	
}

function logoutout(o)
{
	if (browser.ie)
		o.style.backgroundImage = "";
	else
		o.setStyle('background-image', '');	
    //$j('#logoutword').css('color', '#000');
}

function frontpageClick()
{
    self.location='index.php';
}

function logoutclick()
{
	window.location.href="logout.php";	
}

function renameClick(o)
{
    var message = "确定要将\"" + $("fav_type" + o.id.substring(4)).innerHTML + "\"重命名为";
    var result = prompt(message, "列表名");
    if (result)
    {
    	var xmlhttp;
    	if (window.XMLHttpRequest)
      	{// code for IE7+, Firefox, Chrome, Opera, Safari
      		xmlhttp=new XMLHttpRequest();
      	}
    	else
      	{// code for IE6, IE5
      		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      	}
    	xmlhttp.onreadystatechange=function()
      	{
      		if (xmlhttp.readyState==4 && xmlhttp.status==200)
        	{
    			if (xmlhttp.responseText == "true")
    			{
                    $("fav_type" + o.id.substring(4)).innerHTML = result;
    			}
    			else
    			{
                    alert(xmlhttp.responseText);
    			}
        	}
      	}
    	xmlhttp.open("POST","renameFavList.php",true);
    	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
    	xmlhttp.send("origin=" + $("fav_type" + o.id.substring(4)).innerHTML + "&changeto=" + result);
    }
}

function deleteClick(o)
{
    var message = "确定要将\"" + $("fav_type" + o.id.substring(6)).innerHTML + "\"列表删除？";
    var result = confirm(message);
    if (result)
    {
    	var xmlhttp;
    	if (window.XMLHttpRequest)
      	{// code for IE7+, Firefox, Chrome, Opera, Safari
      		xmlhttp=new XMLHttpRequest();
      	}
    	else
      	{// code for IE6, IE5
      		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      	}
    	xmlhttp.onreadystatechange=function()
      	{
      		if (xmlhttp.readyState==4 && xmlhttp.status==200)
        	{
    			if (xmlhttp.responseText == "true")
    			{
                    $("favList" + o.id.substring(6)).destroy();
    			}
    			else
    			{
                    alert(xmlhttp.responseText);
    			}
        	}
      	}
    	xmlhttp.open("POST","deleteFavList.php",true);
    	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
    	xmlhttp.send("fav_type=" + $("fav_type" + o.id.substring(6)).innerHTML);
    }
}

function createList()
{
    var id = ($j('.edit').length);
    var result = prompt("请输入列表名", "列表名");
    if (result)
    {
        addAFavList(result, id + 1);
    }
}

function addAFavList(listname, id)
{
	var xmlhttp;
	if (window.XMLHttpRequest)
  	{// code for IE7+, Firefox, Chrome, Opera, Safari
  		xmlhttp=new XMLHttpRequest();
  	}
	else
  	{// code for IE6, IE5
  		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	xmlhttp.onreadystatechange=function()
  	{
  		if (xmlhttp.readyState==4 && xmlhttp.status==200)
    	{
			if (xmlhttp.responseText.indexOf(':') != -1)
			{
                var favlist = new Element('div', {'id':'favList'+id, 'class':'favList'}).inject($('favLists'));
                var favlistleft = new Element('div', {'class':'favList_left'}).inject(favlist);
                favlistleft.innerHTML = "<a href='index.php?fav_type=" + listname + "'><span id='fav_type" + id + "'>" + listname + "</span></a> ( 0 ) 创建时间：  " + xmlhttp.responseText;
                favlist.innerHTML += "<div id='edit" + id + "' class='edit' onclick='renameClick(this)'>重命名</div>";
                favlist.innerHTML += "<div id='delete" + id  + "' class='delete' onclick='deleteClick(this)'>删除</div>";
			}
			else
			{
                alert(xmlhttp.responseText);
			}
    	}
  	}
	xmlhttp.open("POST","addNewFavList.php",true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	xmlhttp.send("listname=" + listname);
}
